﻿using System;
using System.Linq;
using System.Text;
using ECode.ECommon.Helpers;
using ECode.EComPort.Base;
using ECode.EComPort.Common;

namespace ECode.ModbusTester.Lib.ViewModel
{
    public class SerialPortViewModel : BaseViewModel
    {
        #region property

        /// <summary>
        /// 串口名
        /// </summary>
        public string ComName { get; set; }
        /// <summary>
        /// 波特率
        /// </summary>
        public string Baud { get; set; }
        /// <summary>
        /// 数据位
        /// </summary>
        public string DataBit { get; set; }
        /// <summary>
        /// 停止位
        /// </summary>
        public string StopBit { get; set; }
        /// <summary>
        /// 奇偶校验位
        /// </summary>
        public string Parity { get; set; }
        /// <summary>
        /// 通信数据编码方式
        /// </summary>
        public string ComDataType { get; set; }
        /// <summary>
        /// 波特率选项
        /// </summary>
        public string[] BaudItems { get { return ESerialPort.BaudRateArr.Select(t => t.ToString()).ToArray(); } }
        /// <summary>
        /// 数据位选项
        /// </summary>
        public string[] DataBitItems { get { return ESerialPort.DataBitsArr.Select(t => t.ToString()).ToArray(); } }
        /// <summary>
        /// 停止位选项
        /// </summary>
        public string[] StopBitItems { get { return ESerialPort.StopBitArr; } }
        /// <summary>
        /// 奇偶校验位选项
        /// </summary>
        public string[] ParityItems { get { return ESerialPort.ParityArr; } }
        /// <summary>
        /// 获取串口列表
        /// </summary>
        public string[] ComNames { get { return HardwareHelper.GetComs(); } }
        /// <summary>
        /// 通信数据编码方式
        /// </summary>
        public string[] EncodingType { get { return Enum.GetNames(typeof(ComInfoDataType)); } }

        private string _msg;
        /// <summary>
        /// 消息
        /// </summary>
        public string Msg { get { return _msg; } private set { _msg = value; OnPropertyChanged("Msg"); } }
        /// <summary>
        /// 串口对象
        /// </summary>
        public ESerialPort Com { get; set; }

        #endregion

        #region Field



        #endregion

        #region

        public delegate void OnReceviedHandler(string data, int len);

        public event OnReceviedHandler OnRecevied;
        #endregion

        #region public
        /// <summary>
        /// 打开串口
        /// </summary>
        public void Open()
        {
            if (Com == null)
                Com = new ESerialPort();
            else
                Com.Close();

            Msg = Com.Msg;

            Com.PortName = ComName;
            Com.BaudRate = int.Parse(Baud);
            Com.DataBits = byte.Parse(DataBit);
            Com.StopBit = StopBit;
            Com.ParityItem = Parity;
            Com.ComDataType = (ComInfoDataType)Enum.Parse(typeof(ComInfoDataType), ComDataType);
            Com.OnDataReceived += OnDataReceived;
            Com.SetConfig();

            Com.Open();

            Msg = Com.Msg;
        }

        private void OnDataReceived(byte[] data)
        {
            if (OnRecevied != null)
                OnRecevied(Com.LastDataStr,data.Length);
        }

        /// <summary>
        /// 关闭串口
        /// </summary>
        public void Close()
        {
            if (Com == null) return;
            Com.Close();
            Msg = Com.Msg;
        }

        #endregion
    }
}
